package jetzhu;

import java.util.Scanner;

public class Merge {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        int[] str = getArray1();
        int[] str1 = getArray2();
        int[] str2 = merge(str, str1);
        System.out.println("str is " + printArray(str));
        System.out.println("str1 is " + printArray(str1));
        System.out.println("The merged list  is " + printArray(str2));

    }

    public static int[] getArray1() {
        System.out.println("Enter str size and contents:");
        int n = input.nextInt();
        int[] str = new int[n];
        for (int i = 0; i < str.length; i++) {
            str[i] = input.nextInt();
        }
        return str;
    }

    public static int[] getArray2() {
        System.out.println("Enter str1 size and contents:");
        int n2 = input.nextInt();
        int[] str1 = new int[n2];
        for (int i = 0; i < str1.length; i++) {
            str1[i] = input.nextInt();
        }
        return str1;
    }

    public static int[] merge(int[] list1, int[] list2) {
        int sum = list1.length + list2.length;
        int[] str = new int[sum];
        int i = 0;
        int j = 0;
        int k = 0;
        while (i < list1.length && j < list2.length) {
            if (list1[i] < list2[j]) {
                str[k] = list1[i];
                i++;
            } else {
                str[k] = list2[j];
                j++;
            }
            k++;
        }
        while (i < list1.length) {
            str[k++] = list1[i++];
        }
        while (j < list2.length) {
            str[k++] = list1[j++];
        }
        return str;
    }

    public static StringBuilder printArray(int[] list) {
        StringBuilder s = new StringBuilder("");
        for (int j : list)
            s.append(j)
                    .append(" ");
        System.out.println();
        return s;
    }
}
